package day_2023_7_to_10;

/**
 * @author haomin
 * @date 2023/08/14 23:15
 **/
public class Day_8_14 {
    class Solution {
        public int maxSubArray(int[] nums) {
            if(nums.length == 1) return nums[0];
            int max = nums[0];
            for(int i = 1; i < nums.length; ++i) {
                if (nums[i] + nums[i-1] > nums[i]) nums[i] = nums[i] + nums[i-1];
                max = Math.max(max, nums[i]);
            }
            return max;
        }
    }
    class Solution2 {
        public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
            if(root1 == null && root2 == null) return null;
            if(root1 == null) return root2;
            if(root2 == null) return root1;
            TreeNode cur = new TreeNode(root1.val + root2.val);
            cur.left = mergeTrees(root1.left, root2.left);
            cur.right = mergeTrees(root1.right, root2.right);
            return cur;
        }
    }
    class Solution3 {
        public void merge(int[] nums1, int m, int[] nums2, int n) {
            if(n == 0) return;
            int l = m + n;
            int i = m-1, j = n-1;
            for(int p = l-1; p >= 0;) {
                System.out.println(i+"---"+j);
                if(i == -1) {
                    nums1[p] = nums2[j];
                    p--;
                    j--;
                }else if(j == -1) {
                    nums1[p] = nums1[i];
                    p--;
                    i--;
                }else if(nums1[i] > nums2[j]) {
                    nums1[p] = nums1[i];
                    p--;
                    i--;
                }else {
                    nums1[p] = nums2[j];
                    p--;
                    j--;
                }
            }
            return;
        }
    }
}